function aaa() {
    console.log("====")
}

var token = 'E7BFA2557AE0DD03F4D83ED5BC6930C3';
var topWindow = {
    loginResult: {
        'access_token': token
    }
}
var isInCef = false;


// 生成签名
var signInfo = {
    // 吉客云
    jackyun: function () {
        return {
            token: token,
            appkey: "jackyun_web_browser_2024",
            secret: "72EyvujHoQWmjfKqsl168SaVycZARQvt",
        }
    },
    // 管家J系列
    jackyun_wdgj: function () {
        if (isInCef) {
            return {
                token: window.get_token() || "",
                appkey: "wdgj_j_client",
            }
        }
        return {
            token: jkUtils.cookie.getCookie('token') || "",
            appkey: "wdgj_j_web_browser",
            secret: "rObVimzXWyslxyqDxacwpgXpbNZAWiv6",
        }
    },
    // 数字工厂
    jackyun_ali: function () {
        return {
            token: jkUtils.cookie.getCookie('token') || "",
            appkey: "jackyun_iot",
            secret: "3MG5TnCcvJLbihKrxLvKYa4GCCVCmGQd",
        }
    },
    // 京东平台
    jd_web: function () {
        return {
            token: sessionStorage.getItem("token") || "",
            appkey: "jackyun_jd_web",
            secret: "dwJs8RD7223HDkeu2fSH7EDs",
        }
    }
}

function jkJsonStringify(param, spaces) {
    return JSON.stringify(param, serializer(), spaces);

    function serializer() {
        let stack = [];
        return function (key, value) {
            if (stack.length > 0) {
                let thisPos = stack.indexOf(this);
                ~thisPos ? stack.splice(thisPos + 1) : stack.push(this)
                if (~stack.indexOf(value)) {
                    value = undefined; //忽略循环引用项
                }
            } else {
                stack.push(value);
            }
            return value;
        }
    }
}

function asciiSort(a, b, key, flag) {
    if (typeof a === 'object') {
        a = a[key];
        b = b[key];
    }
    if (typeof a === 'string') flag = key;

    if (a < b) {
        if (flag === false) return 1;
        return -1;
    }
    if (a > b) {
        if (flag === false) return -1;
        return 1;
    }
    return 0;
}

function isInJkyunCef() {
    if (isInCef) {
        let version = JSON.parse(versionDetail);
        if (version.wdgj == 0 || !topWindow.isHighversion) return true;
        if (window.login_site === 'login_site') return true;
    }
    return localStorage.getItem("login_site") === 'jackyun';
}

function extend(target, ...sources) {
    // 如果 target 是 undefined 或 null，直接返回空对象
    if (target == null) {
        target = {};
    }

    // 遍历所有源对象
    for (const source of sources) {
        // 如果源对象是 undefined 或 null，跳过
        if (source == null) {
            continue;
        }

        // 遍历源对象的所有属性
        for (const key in source) {
            // 确保属性是源对象自身的属性（而不是继承的）
            if (Object.prototype.hasOwnProperty.call(source, key)) {
                // 将属性复制到目标对象
                target[key] = source[key];
            }
        }
    }
    // 返回合并后的目标对象
    return target;
}

function jkGetSign2(e, t, n, i) {
    let o = {},
        a = [],
        r = void 0 === n || n;
    try {
        let e = 'jackyun';
        (!0 === i || false && "jkyun" === topWindow.signPlate) && (e = "jackyun");

        let t = signInfo[e]();
        if (!0 === i && (t.secret = "app_jackyun_manager_secret"),
            !t)
            return n ? "" : null;
        var s = t.token,
            l = t.appkey,
            c = t.secret
    } catch (e) {
        return n ? "" : null
    }
    void 0 !== e && (o = extend(o, e));
    var d = {
        key: "appkey"
    };
    d.value = l,
        a.push(d);
    var u = {
        key: "timestamp"
    };
    u.value = String((new Date).valueOf()),
        a.push(u);
    var f = {
        key: "access_token"
    };
    for (var m in f.value = "Bearer " + (s || topWindow.loginResult.access_token || ""),
        a.push(f),
        e) {
        var p = {
                key: m
            },
            g = e[m];
        void 0 === g || "string" == typeof g && g.match(/^\s+$/) || "" === g || (/\W/g.test(m) && console
            .error("接口请求参数名key请不要使用特殊字符，可使用字母数字_"),
            p.value = "object" == typeof g ? jkJsonStringify(g) : String(g),
            a.push(p))
    }

    var h, k = null == t ? "" : t;
    if (isInCef && !0 !== i)
        try {
            var y = JSON.stringify({
                data: a
            });
            console.log("==hh==", h);
            // h = window.make_sign(y, k).toUpperCase()
        } catch (e) {

        }
    else
        console.log("签名", a, c, k)
    console.log("准备方式", 'r-', r, 'u-', u, 'f-', f, 'd-', d, 'h-不需要', h, 'o-', o)

    // h = makeSign(a, c, k);



    if (r) {
        var w = "timestamp=" + u.value;
        for (var j in w = (w = (w = w + "&access_token=" + f.value) + "&appkey=" + d.value) + "&sign=" + h,
            o)
            w = "object" == typeof o[j] && null !== o[j] ? w + "&" + j + "=" + String(encodeURIComponent(
                jkJsonStringify(o[j]))) : w + "&" + j + "=" + String(encodeURIComponent(String(o[
                j])));
        console.error("走到这里算对的", w)
        return w
    }

    for (var j in o)
        "object" == typeof o[j] && null !== o[j] ? o[j] = jkUtils.jkJsonStringify(o[j]) : o[j] = String(o[
            j]);
    return o.timestamp = u.value,
        o.access_token = f.value,
        o.appkey = d.value,
        o.sign = h,
        o
}

// 生成签名
function makeSign(params, secret) {
    console.log("生成签名", params, secret)
    if (!params) params = {};
    var str = '',
        _this = this;
    var arr = params.sort(function (a, b) {
        return asciiSort(a, b, 'key');
    });
    console.log("=arrarrarrarr=1", arr);
    arr.forEach(a => {
        str += a.key + a.value;
    });

    console.log("=arrarrarrarr=2", str);

    console.log("-secretsecret-", secret + str + secret)
    console.log("-secretsecret-", md5(secret + str + secret))
    var sign = md5(secret + str + secret).toUpperCase();
    console.log("我是md5", sign);
    return sign;
}

function sign_pinjie(r, u, f, d, h, o) {
    if (r) {
        var w = "timestamp=" + u.value;
        for (var j in w = (w = (w = w + "&access_token=" + f.value) + "&appkey=" + d.value) + "&sign=" + h,
            o)
            w = "object" == typeof o[j] && null !== o[j] ? w + "&" + j + "=" + String(encodeURIComponent(
                jkJsonStringify(o[j]))) : w + "&" + j + "=" + String(encodeURIComponent(String(o[
                j])));
        console.error("走到这里算对的", w)
        return w
    }
}

